<?php 
if(KT!='KhiThe') exit;
class contacts_controller extends contacts_model{
		
	public function __construct(){
		global $lang;
		$lang['url_base'] = url_base;
		$this->lang = $lang;
		
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_paging');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
	}
	
	public function index_action()
	{
		$resultHtml = '';
		$limit=25;
        $page = intval($this->valUrl(1));
		$totalRows = mysql_num_rows(parent::getcontacts('',''));
		$strPaging = $this->display_paging($totalRows, empty($page)?1:$page, intval($limit), url_base.'admin/contacts/index', $this->lang,'/');
		$this->lang['paging'] = $strPaging;
		$strHtmlContent = $this->khithe_display('index');
		$this->arrBlockHtml = $this->khithe_getBlockHtml($strHtmlContent);
        $this->lang['page'] = empty($page)?1:$page;
		$resultcontact = parent::getcontacts(intval($this->valUrl(1)), intval($limit));
		while( $row_contacts = mysql_fetch_assoc($resultcontact) )
		{
			$arrcontact = array(
				"page"=>$this->valUrl(1),
				"cell_ID"=>$row_contacts['ID'],
				"cell_1"=>'<input name="delId[]" type="checkbox" value="'.$row_contacts['ID'].'" />',
				"cell_2"=>$this->cutString($row_contacts['Title'], 30),
				"cell_2_t"=>$row_contacts['Title'],
				"cell_3"=>$row_contacts['Fullname'],
				"cell_4"=>$row_contacts['Telephone'],
				"cell_5"=>$row_contacts['Email'],
				"cell_6"=>$row_contacts['PostDate']!=''?date("d-m-Y",$row_contacts['PostDate']):'',
				"cell_7"=>$row_contacts['Status']=='true'?$this->lang['enabled']:$this->lang['disabled'],
				"class_cell"=>$row_contacts['Status']=='false'?"cell_groupitem1":"cell_groupitem_ex1",
			);
			$resultHtml .= $this->khithe_assign($this->arrBlockHtml['contacts'],$arrcontact);
			
		} mysql_free_result($resultcontact);
		
		$strHtmlContent = $this->khithe_assignBlock($strHtmlContent, array('htmlCat'=>$resultHtml));
		$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
		
		return $strHtmlContent;
	}
	
	public function detail_action()
	{
		if($_GET['iframe']=='true')
		{
			$resultcontact = parent::getcontactID($this->valUrl(1));
			$row_contacts = mysql_fetch_assoc($resultcontact);
			$strHtmlContent = $this->khithe_display('detail');
			$this->lang['titlePageEditor'] = isset($this->lang['editTitle'])?$this->lang['editTitle']:NULL;
			$this->lang['page'] = $this->valUrl(2);
			$this->lang['ContactID'] = $row_contacts['ID'];
			$this->lang['Content'] = $row_contacts['Content'];
			$this->lang['PostDate'] = $row_contacts['PostDate']!=''?date('d-m-Y',$row_contacts['PostDate']):'';
			$this->lang['title'] = $row_contacts['Title'];
			$this->lang['Fullname'] = $row_contacts['Fullname'];
			$this->lang['Telephone'] = $row_contacts['Telephone'];
			$this->lang['Address'] = $row_contacts['Address'];
			$this->lang['Email'] = $row_contacts['Email'];
			
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function editor_action()
	{
		if(isset($_POST['txt_textarea']) && $_POST['txt_textarea']!='')
		{
			@file_put_contents(path_app.'/admin/com_contacts/views/contacts.phtml',(string)$_POST['txt_textarea']);
		}
		if(file_exists(path_app.'/admin/com_contacts/views/contacts.phtml')){
			$strcontact = stripslashes(file_get_contents(path_app.'/admin/com_contacts/views/contacts.phtml'));
		
		}
		$this->lang['txt_textarea'] = isset($_POST['txt_textarea'])?$_POST['txt_textarea']:$strcontact;
		$strHtmlContent = $this->khithe_display('editor');
		
		return $strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
	
	}
	
	public function index_ajax_action()
	{
		switch ($this->valUrl(2))
		{
			case 'open':
				$arr = explode(',',$this->valUrl(3));
				foreach ( $arr as $ID ){
					$select = parent::getcontactID($ID);
					while($row = mysql_fetch_assoc($select)){
						if($row['Status']=='false'){
							$done = parent::setCheckedcontact($ID,'true');
						}
					}
				}
			break;
			
			case 'del':
				$arr = explode(',',$this->valUrl(3));
                foreach ( $arr as $ID ){
					$done = parent::delCheckedcontact($ID);	
				}
			break;
		}
		if($this->valUrl(2)!="" && isset($done)){
			return $this->index_action();
			
		}else{
			return 'notOK';
		}	
	}


}
?>